// pages/qualityTesting/qualityTesting.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
let load = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    engineersInfo:'',
    login_status: false,
    is_load:false,

    navbarInitTop: 0, //导航栏初始化距顶部的距离
    isFixedTop: false, //是否固定顶部

    isMaskShow: false,//遮罩层显隐
    move: 0,//筛选弹窗
    screenShow: false,//筛选模块显隐,
    keywords: '',//关键字
    dateStart: '',//筛选开始时间
    dateEnd: '',//结束时间

    dataList: [],//业绩列表
    empty: true,
    hasMore: false,
    loading: false,
    loadShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.hidebuttonFun();
    that.getAccountInco();//获取账户信息
    _PAGE = 1;
    that.getList();//获取业绩列表
  },
  //现获取基础库版本
  hidebuttonFun() {
    wx.getSystemInfo({
      success: function (res) {
        var version = res.SDKVersion;
        version = version.replace(/\./g, "")
        console.log('结果', version, parseInt(version) >= 283, wx.canIUse('hideHomeButton'))
        if (parseInt(version) >= 283 && wx.canIUse('hideHomeButton')) {
          wx.hideHomeButton();
        }
      }
    })
  },

  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    var that = this;
    app.loginStatus(function () {
      that.getEngMsg();
      that.setData({
        login_status: true,
        is_load: true
      })
    }, function () {
      that.setData({
        login_status: false,
        engineersInfo: '',
        is_load: true

      })
    })

    //3秒后自动打开
    setTimeout(() => {
      that.setData({
        is_load: true
      })
    }, 3000)

    // ---------筛选吸顶处理----------
    if (that.data.navbarInitTop == 0) {
      //获取节点距离顶部的距离
      wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
        if (rect && rect.top > 0) {
          var navbarInitTop = parseInt(rect.top);
          that.setData({
            navbarInitTop: navbarInitTop
          });
        }
      }).exec();
    }
  },

  /**
   * 监听页面滑动事件
   */
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度

    //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
    var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
    //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
    if (that.data.isFixedTop === isSatisfy) {
      return false;
    }

    that.setData({
      isFixedTop: isSatisfy
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.getAccountInco();//获取账户信息
    _PAGE = 1;
    that.getList();//获取业绩列表
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    _PAGE++;
    that.setData({
      loading: true
    })
    that.getList();
  },
  
  //获取用户信息
  getEngMsg: function () {
    var that = this;
    app.ajax({
      url: "Engineer/Engineers/engineersInfo",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            engineersInfo: res.data.data
          })
        } else {
          that.setData({
            engineersInfo: ''
          })
        }
      }
    })
  },

  //跳转用户信息
  userMsg() {
    wx.navigateTo({
      url: '/pages/myCenter/userMsg/userMsg',
    })
  },

  //请先登录
  loginHint(){
    var that=this;
    app.showToast('请先登录');
    setTimeout(()=>{
      that.goLogin();
    },1000)
  },

   //跳转登录
  goLogin() {
    wx.navigateTo({
      url: '../login/login',
    })
  },

  // 获取账户信息
  getAccountInco() {
    var that = this;
    app.ajax({
      url: "Engineer/Account/engineers",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            accountInfo: res.data.data,
          })
        }
      }
    })
  },
  // 跳转账户提现
  goAccount() {
    wx.navigateTo({
      url: '/pages/myCenter/accounputForward/accounputForward',
    })
  },

  // 获取业绩列表
  getList() {
    var that = this;
    app.ajax({
      url: 'Engineer/Projectperformance/achievementDetailed',
      load: load,
      msg: '数据加载中...',
      data: {
        page: _PAGE,
        pagesize: _PAGESIZE,
        keywords: that.data.keywords,
        add_time: that.data.dateStart,
        end_time: that.data.dateEnd,
      },
      success: function (res) {
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              dataList: res.data.data,
              empty: false
            });
          } else {
            that.setData({
              dataList: that.data.dataList.concat(res.data.data),
              empty: false
            });
          }
          for (var index in that.data.dataList) {
            var isAmountShow = "dataList[" + index + "].isAmountShow";
            var isEngineersShow = "dataList[" + index + "].isEngineersShow";
            that.setData({
              [isAmountShow]: false, //默认隐藏金额明细
              [isEngineersShow]: false, //默认隐藏人员明细
            })
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load = false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          loadShow: true
        })
      }
    })
  },

  // 筛选模块显、隐
  screenTap(){
    var that = this;
    if (!that.data.screenShow) {
      if (!that.data.isFixedTop){
        wx.pageScrollTo({
          selector: '#navbar',
          duration: 300
        })
      }
      that.setData({
        screenShow: true,
        isMaskShow: true,
        move: 0
      })
    } else {
      that.setData({
        move: 1,
      })
      setTimeout(function () {
        that.setData({
          screenShow: false,
          isMaskShow: false,
        })
      }, 500)
    }
  },
  // 关闭筛选弹窗
  closeMask() {
    var that = this;
    that.setData({
      move: 1,
    })
    setTimeout(function () {
      that.setData({
        screenShow: false,
        isMaskShow: false,
      })
    }, 500)
  },
  //输入框输入
  inputChange(e){
    var that = this;
    var val = e.detail.value;
    var name = e.currentTarget.dataset.name;
    that.setData({
      [name]: val
    })
    console.log(123, e,that.data.keywords)
  },
  //选择开始时间
  dateStartChange(e){
    var that = this;
    that.setData({
      dateStart: e.detail.value
    })
  },
  //选择结束时间
  dateEndChange(e) {
    var that = this;
    that.setData({
      dateEnd: e.detail.value
    })
  },
  //重置
  reset(){
    var that = this;
    that.setData({
      keywords: '',
      dateStart: '',//筛选开始时间
      dateEnd: '',//结束时间
    })
  },
  //搜索
  sureChoose(){
    var that = this;
    that.getList();
    that.closeMask();
  },

  //查看、收起积分明细
  lookAmountInfo(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var temp = 'dataList[' + index + '].isAmountShow';
    that.setData({
      [temp]: !that.data.dataList[index].isAmountShow,
    })
  },
  //查看、收起人员明细
  lookEngineersInfo(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var temp = 'dataList[' + index + '].isEngineersShow';
    that.setData({
      [temp]: !that.data.dataList[index].isEngineersShow,
    })
  },
})
